package com.example.clsj.service;

import java.util.Date;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.IdcardUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.sax.handler.RowHandler;
import com.example.clsj.anno.Loggable;
import com.example.clsj.domain.DataY;
import com.example.clsj.domain.HrUserInfo;
import com.example.clsj.dto.Result;
import com.example.clsj.mapper.HrUserInfoMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
@RequiredArgsConstructor
public class HrServiceImpl implements HrService {

    private final HrUserInfoMapper hrUserInfoMapper;


    /**
     * 更新信息方法
     * 该方法接收一个字符串格式的信息，解析后从中提取Excel文件路径，并读取该Excel文件的内容
     * 主要用于处理信息更新请求，将外部的Excel数据导入到系统中
     *
     * @param info 包含Excel文件路径的字符串信息
     * @return 返回操作结果，成功则返回表示成功的Result对象
     */
    @Loggable
    public Result updateInfo(String info) {
        // 解析输入的字符串信息为JSONObject对象
        JSONObject req = JSONUtil.parseObj(info);
        // 提取info字段内的JSONObject对象，其中包含Excel文件路径
        JSONObject reqInfo = req.getJSONObject("info");
        // 从reqInfo中获取Excel文件的路径字符串
        String file = reqInfo.getStr("file");
        String employeeStatus = reqInfo.getStr("employeeStatus");
        // 根据文件路径创建File对象
        File excel = new File(file);

        // 使用SAX方式读取Excel文件，并处理每行数据
        // SAX是一种基于事件的解析方法，适用于大型文件的解析，因为它不需要一次性加载整个文件到内存中
        ExcelUtil.readBySax(excel, -1, createRowHandler(employeeStatus));

        // 返回表示操作成功的Result对象
        return Result.success();
    }


    /**
     * 创建行处理程序
     * 此方法用于生成一个处理表格行数据的函数，主要包含数据解析和存储逻辑
     *
     * @return 返回一个lambda表达式，该表达式定义了如何处理每个表格行的数据
     */
    private RowHandler createRowHandler(String employeeStatus) {
        // 返回一个行处理程序，包含具体的处理逻辑
        return (sheetIndex, rowIndex, rowCells) -> {
            // 记录当前正在处理的行信息
            log.info("[{}] [{}] {}", sheetIndex, rowIndex, rowCells);

            if (rowIndex == 0) return;

            HrUserInfo hrUserInfo = new HrUserInfo();
            hrUserInfo.setJobNumber(String.valueOf(rowCells.get(1)));
            hrUserInfo.setUserName(String.valueOf(rowCells.get(2)));
            hrUserInfo.setCorporateName(String.valueOf(rowCells.get(3)));
            hrUserInfo.setCostCenterName(String.valueOf(rowCells.get(4)));
            hrUserInfo.setOrganizationName(String.valueOf(rowCells.get(5)));
            hrUserInfo.setPosition(String.valueOf(rowCells.get(6)));
            hrUserInfo.setGender(String.valueOf(rowCells.get(7)));
            hrUserInfo.setNation(String.valueOf(rowCells.get(8)));
            hrUserInfo.setNativePlace(String.valueOf(rowCells.get(10)));
            hrUserInfo.setEducation(String.valueOf(rowCells.get(11)));
            hrUserInfo.setEducationalInstitutions(String.valueOf(rowCells.get(12)));
            hrUserInfo.setSpeciality(String.valueOf(rowCells.get(13)));
            hrUserInfo.setPoliticalStatus(String.valueOf(rowCells.get(14)));
            hrUserInfo.setAddress(String.valueOf(rowCells.get(15)));
            hrUserInfo.setEntryDate(StrUtil.isBlank(String.valueOf(rowCells.get(16))) ? null : DateUtil.parseDate(String.valueOf(rowCells.get(16))));
            hrUserInfo.setEmployeeType(String.valueOf(rowCells.get(17)));
            hrUserInfo.setContractStartDate(StrUtil.isBlank(String.valueOf(rowCells.get(18))) ? null : DateUtil.parseDate(String.valueOf(rowCells.get(18))));
            hrUserInfo.setContractEndDate(StrUtil.isBlank(String.valueOf(rowCells.get(19))) ? null : DateUtil.parseDate(String.valueOf(rowCells.get(19))));
            hrUserInfo.setResignationStartDate(StrUtil.isBlank(String.valueOf(rowCells.get(20))) ? null : DateUtil.parseDate(String.valueOf(rowCells.get(20))));
            hrUserInfo.setEnglishLevel(String.valueOf(rowCells.get(21)));
            hrUserInfo.setAnnualEvaluation(String.valueOf(rowCells.get(22)));
            hrUserInfo.setMaritalStatus(String.valueOf(rowCells.get(23)));
            hrUserInfo.setJobLevelText(String.valueOf(rowCells.get(24)));
            hrUserInfo.setSalaryRangeText(String.valueOf(rowCells.get(25)));
            hrUserInfo.setOrganizationCode(String.valueOf(rowCells.get(26)));
            hrUserInfo.setPositionCode(String.valueOf(rowCells.get(27)));
            hrUserInfo.setJobCode(String.valueOf(rowCells.get(28)));
            hrUserInfo.setJobDescription(String.valueOf(rowCells.get(29)));
            hrUserInfo.setHouseholdRegistrationType(String.valueOf(rowCells.get(30)));
            hrUserInfo.setHouseholdRegistration(String.valueOf(rowCells.get(31)));
            hrUserInfo.setHeight(String.valueOf(rowCells.get(32)));
            hrUserInfo.setHouseholdRegistrationBelonging(String.valueOf(rowCells.get(33)));
            hrUserInfo.setEducationType(String.valueOf(rowCells.get(34)));
            hrUserInfo.setEducationMethod(String.valueOf(rowCells.get(35)));
            hrUserInfo.setResignationEndDate(StrUtil.isBlank(String.valueOf(rowCells.get(36))) ? null : DateUtil.parseDate(String.valueOf(rowCells.get(36))));
            hrUserInfo.setResignationJobType(String.valueOf(rowCells.get(37)));
            hrUserInfo.setCostCenterCode(String.valueOf(rowCells.get(38)));
            hrUserInfo.setSeniorityStartDate(StrUtil.isBlank(String.valueOf(rowCells.get(39))) ? null : DateUtil.parseDate(String.valueOf(rowCells.get(39))));
            hrUserInfo.setCorporateSeniorityStartDate(StrUtil.isBlank(String.valueOf(rowCells.get(40))) ? null : DateUtil.parseDate(String.valueOf(rowCells.get(40))));
            hrUserInfo.setSalaryRange(String.valueOf(rowCells.get(41)));
            hrUserInfo.setAccumulationFundArea(String.valueOf(rowCells.get(42)));
            hrUserInfo.setAccumulationFundAreaText(String.valueOf(rowCells.get(43)));
            hrUserInfo.setAccumulationFundIndustry(String.valueOf(rowCells.get(44)));
            hrUserInfo.setSocialSecurityArea(String.valueOf(rowCells.get(45)));
            hrUserInfo.setSocialSecurityAreaText(String.valueOf(rowCells.get(46)));
            hrUserInfo.setSocialSecurityIndustry(String.valueOf(rowCells.get(47)));
            hrUserInfo.setJobLevel(String.valueOf(rowCells.get(48)));
            hrUserInfo.setCorporateCode(String.valueOf(rowCells.get(49)));
            hrUserInfo.setEmployeeStatus(employeeStatus);

            HrUserInfo hrUserInfo1 = hrUserInfoMapper.selectOneByJobNumber(hrUserInfo.getJobNumber());

            if (hrUserInfo1 == null) {
                hrUserInfoMapper.insert(hrUserInfo);
            } else {
                hrUserInfo.setKeyTalentSituation(hrUserInfo1.getKeyTalentSituation());
                hrUserInfo.setCollegeStudentsSituation(hrUserInfo1.getCollegeStudentsSituation());
                hrUserInfo.setReserveTalents(hrUserInfo1.getReserveTalents());
                hrUserInfo.setReservedFieldOne(hrUserInfo1.getReservedFieldOne() == null ? IdcardUtil.getIdcardInfo(String.valueOf(rowCells.get(9))).getBirthDate() : hrUserInfo1.getReservedFieldOne());
                hrUserInfo.setReservedFieldTwo(hrUserInfo1.getReservedFieldTwo());
                hrUserInfo.setReservedFieldThree(hrUserInfo1.getReservedFieldThree());
                hrUserInfo.setReservedFieldFour(hrUserInfo1.getReservedFieldFour());
                hrUserInfo.setReservedFieldFive(hrUserInfo1.getReservedFieldFive());
                hrUserInfo.setReservedFieldSix(hrUserInfo1.getReservedFieldSix());
                hrUserInfo.setReservedFieldSeven(hrUserInfo1.getReservedFieldSeven());
                hrUserInfo.setReservedFieldEight(hrUserInfo1.getReservedFieldEight());
                hrUserInfo.setReservedFieldNine(hrUserInfo1.getReservedFieldNine());
                hrUserInfo.setReservedFieldTen(hrUserInfo1.getReservedFieldTen());
                hrUserInfo.setEmployeeStatus(hrUserInfo1.getEmployeeStatus());
                hrUserInfo.setId(hrUserInfo1.getId());
                hrUserInfoMapper.updateByPrimaryKey(hrUserInfo);
            }

        };
    }

}
